/*
 * @Author: 张小平
 * @Date:   2025-05-01
 * @Email:  qqshuqian@163.com
 * @Copyright © 2024 aseyun. All rights reserved.
 */
// import UserSelect, { RoleSelect } from '@/framework-code/components/user'
import userChoose from '@/framework-core/components/user/user/userChoose.vue'
import orgTreeChoose from '@/framework-core/components/user/org/orgTreeChoose.vue'
import roleTreeChoose from '@/framework-core/components/user/role/roleTreeChoose.vue'
import _ from 'lodash'
import { assignType } from '@/framework-code/utils/constants'
import taskAPI from '@/framework-code/api/task'
let initRoleInfo = {
  id: null,
  name: null,
  relateStartOrgId: false
}
const assignMember = {
  components: {
    userChoose,
    orgTreeChoose,
    roleTreeChoose
  },
  data() {
    let initPersonType = assignType.t1
    let initUserDatas = []
    let userDatasIdsTemp = [];
    let initContactValue = ''
    if (this.item?.properties?.personList?.[0]) {
      const {
        assignMember,
        roleInfo,
        fieldContact,
        personType
      } = this.item.properties.personList[0]

      if (typeof assignMember === 'string') {
        initUserDatas = JSON.parse(assignMember)
      }
      // 避免之前的报错
      if (typeof initUserDatas === 'string') {
        let idstemp = initUserDatas;
        initUserDatas = [];
        initUserDatas.push(idstemp);
      }
      if(initUserDatas){
        initUserDatas.forEach(ele => {
          userDatasIdsTemp.push(ele.id)
        })
      }

      if (typeof personType === 'string') {
        initPersonType = personType
      }


      if (typeof roleInfo === 'string') {
        initRoleInfo = JSON.parse(roleInfo)
      }else{
        initRoleInfo = roleInfo;
      }

      if(initRoleInfo == null){
        initRoleInfo ={};
      }

      if (typeof fieldContact === 'string') {
        const fieldContactObj = JSON.parse(fieldContact)
        initContactValue = fieldContactObj.prop
      }
    }
    return {
      assignType,
      personType: initPersonType,
      roleInfo: initRoleInfo,
      userDataIds:userDatasIdsTemp,
      userDatas: initUserDatas,
      contactValue: initContactValue,
      fieldContacts: [],
      fieldContact: {},
      dialogUser: false,
      dialogVisible: false
    }
  },
  created() {

  },
  watch: {
    roleInfo: {
      immediate: true,   //
      deep: true,
      handler(val) {

        if (this.item?.properties?.personList) {
          this.item.properties.personList[0].roleInfo = JSON.parse(JSON.stringify(val));
        }

        this.$emit('on-item', this.item)
      }
    },
    personType:{
      immediate: true,   //
      deep: true,
      handler (val, oldVal) {
        this.genPersonList()
        if (this.item?.properties?.personList) {
          this.item.properties.personList[0].personType = val
        }
       // alert(JSON.stringify( this.item.properties.personList[0]))
        if (val === assignType.t2 && this.procId) {
          this.formContactPerson(this.procId)
        }

        this.$emit('on-item', this.item)
      }
    },

    userDatas(val) {
      const assignMember = JSON.stringify(val)

      this.genPersonList()

      if (this.item?.properties?.personList) {
        this.item.properties.personList[0].assignMember = assignMember
      }

      this.userDatasIds = [];
      val.forEach(ele => {
        this.userDatasIds.push(ele.id)
      })
      this.$emit('on-item', this.item)


    },
    contactValue(prop) {
      if (this.fieldContacts.length) {
        const fieldContact = _.filter(this.fieldContacts, { prop })[0]

        if (this.item?.properties?.personList) {
          this.item.properties.personList[0].fieldContact = JSON.stringify({
            prop: fieldContact.prop,
            label: fieldContact.label
          })
        }
        this.$emit('on-item', this.item)
      }
    }
  },
  methods: {
    async formContactPerson(procId) {
      try {
        const { data } = await taskAPI.apiFormContactPerson({ procId })

        this.fieldContacts =   data.data.data
      } catch (err) {
        this.$message.error(err.msg)
      }
    },

    genPersonList() {
      if (!this.item?.properties) {
        this.item.properties = {}
      }

      if (!this.item?.properties?.personList) {
        this.item.properties.personList = [{}]
      }
    }
  }
}

export default assignMember
